﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.IO;
using System.Drawing;
using System.Windows.Forms;
using PixelsGraph;

namespace PixelsGraph
{
    class Data
    {
        public Data()
        {
            FunctionList = new BindingSource();
        }

        public BindingSource FunctionList
        {
            get;
            set;
        }

        public bool Save(string FileName)
        {
            bool flag = true;
            StreamWriter writer = null;
            try
            {
                if (File.Exists(FileName))
                {
                    writer = new StreamWriter(File.Open(FileName, FileMode.Truncate), Encoding.UTF8);
                }
                else
                {
                    writer = new StreamWriter(File.Open(FileName, FileMode.CreateNew), Encoding.UTF8);
                }

                foreach (Function f in FunctionList)
                {
                    string s = string.Format("{0},{1},{2},{3},{4}", (int)f.FunctionType, (int)f.FunctionForm, f.Color.Name, f.Note, f.Values.Length);

                    foreach (float value in f.Values)
                    {
                        s += string.Format(",{0}", value);
                    }
                    writer.WriteLine(s);
                }

            }
            catch (Exception e)
            {
                flag = false;
                MessageBox.Show(string.Format("Không thể ghi dữ liệu vào tệp!\r\n\r\nLỗi: {0}", e.Message), "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return flag;
            }
            finally
            {
                if (writer != null)
                {
                    writer.Close();
                }
            }

            return flag;
        }

        public bool Load(string FileName)
        {
            if (!File.Exists(FileName))
            {
                return false;
            }

            bool flag = true;
            StreamReader reader = null;
            try
            {
                reader = new StreamReader(File.OpenRead(FileName));
            }
            catch (Exception e)
            {
                MessageBox.Show(string.Format("Không thể mở tệp!\r\n\r\nLỗi: {0}\r\nĐường dẫn: {1}", e.Message, FileName), "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            string a = null;

            try
            {
                do
                {
                    a = reader.ReadLine();
                    if (string.IsNullOrEmpty(a))
                    {
                        break;
                    }

                    Function funct = new Function();

                    int index1 = a.IndexOf(',');
                    LoaiHam type = (LoaiHam)int.Parse(a.Substring(0, index1));

                    int index2 = a.IndexOf(',', index1 + 1);
                    funct.FunctionForm = (DangHam)int.Parse(a.Substring(index1 + 1, index2 - index1 - 1));

                    int index3 = a.IndexOf(',', index2 + 1);
                    funct.Color = Color.FromName(a.Substring(index2 + 1, index3 - index2 - 1));

                    int index4 = a.IndexOf(',', index3 + 1);
                    funct.Note = a.Substring(index3 + 1, index4 - index3 - 1);

                    int index5 = a.IndexOf(',', index4 + 1);
                    int n = int.Parse(a.Substring(index4 + 1, index5 - index4 - 1));

                    float[] values = new float[n];

                    int index6 = 0;
                    for (int i = 0; i < n && index5 != -1; i++)
                    {
                        index6 = a.IndexOf(',', index5 + 1);

                        if (index6 != -1)
                        {
                            values[i] = float.Parse(a.Substring(index5 + 1, index6 - index5 - 1));
                        }
                        else
                        {
                            values[i] = float.Parse(a.Substring(index5 + 1));
                        }
                        index5 = index6;
                    }

                    funct.SetFunctionParamsAndType(type, values);

                    FunctionList.Add(funct);

                } while (a != null);
            }
            catch (Exception e)
            {
                flag = false;
                MessageBox.Show(string.Format("Dữ liệu trong tệp đã bị lỗi!\r\n\r\nLỗi: {0}", e.Message), "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                FunctionList.Clear();
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }

            return flag;
        }

        public void Clear()
        {
            if (FunctionList != null)
            {
                FunctionList.Clear();
            }
        }
    }
}
